package org.jeecg.modules.aliyun;

import com.aliyun.imageaudit20191230.models.ScanImageAdvanceRequest;
import com.aliyun.imageaudit20191230.models.ScanImageResponse;
import com.aliyun.imageaudit20191230.models.ScanImageResponseBody;

import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;

public class AuditImageUtil {

    /**
     * <b>description</b> :
     * <p>使用AK&amp;SK初始化账号Client</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.imageaudit20191230.Client createClient() throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("LTAI5tNvVHUY6RPWZhv1kaxb")
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("48s6HtXqL8pM0e2i2DJ3jAamIsg16q");
        // Endpoint 请参考 https://api.aliyun.com/product/imageaudit
        config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
        return new com.aliyun.imageaudit20191230.Client(config);
    }

    public static String auditImageByAliyun (String url) throws Exception {
        //java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.imageaudit20191230.Client client = AuditImageUtil.createClient();

        if(!url.contains("https") && url.contains("http")){
            url = url.replace("http","https");
        }


        URL imageUrl = new URL(url);
        InputStream inputStream = imageUrl.openConnection().getInputStream();
        //List<scanImageAdvanceRequest.ScanImageAdvanceRequestTask> taskList = new ArrayList<>();
        ScanImageAdvanceRequest.ScanImageAdvanceRequestTask task = new ScanImageAdvanceRequest.ScanImageAdvanceRequestTask();
        task.setImageURLObject(inputStream);
//        task.setDataId(uuID.randomuuID().tostring());
//        task.setImageTimeMillisecond(1L);
//        task.setInterval(1);
//        task.setMaxFrames(1);
//        taskList.add(task):

//        com.aliyun.imageaudit20191230.models.ScanImageRequest.ScanImageRequestTask task0 = new com.aliyun.imageaudit20191230.models.ScanImageRequest.ScanImageRequestTask()
//                .setImageURL(encodedUrl);
//        com.aliyun.imageaudit20191230.models.ScanImageRequest scanImageRequest = new com.aliyun.imageaudit20191230.models.ScanImageRequest()
//                .setTask(java.util.Arrays.asList(
//                        task0
//                )).setScene(java.util.Arrays.asList(
//                        "porn"
//                 ));

        ScanImageAdvanceRequest scanImageAdvanceRequest = new ScanImageAdvanceRequest()
                .setTask(java.util.Arrays.asList(
                        task
                )).setScene(java.util.Arrays.asList(
                        "porn"
                 ));

        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {

            ScanImageResponse scanImageResponse = client.scanImageAdvance(scanImageAdvanceRequest,runtime);
            //String result = client.scanImageWithOptions(scanImageRequest, runtime).getBody().getData().results.get(0).subResults.get(0).suggestion;
            String result = scanImageResponse.getBody().getData().results.get(0).subResults.get(0).suggestion;

            System.out.println("scanImage result=========="+result);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(ex.getMessage());
        }
        return null;
    }

}
